package com.zhny.project.txt.gather.controller;

import com.zhny.common.utils.poi.ExcelUtil;
import com.zhny.framework.aspectj.lang.annotation.Log;
import com.zhny.framework.aspectj.lang.enums.BusinessType;
import com.zhny.framework.web.controller.BaseController;
import com.zhny.framework.web.domain.AjaxResult;
import com.zhny.framework.web.page.TableDataInfo;
import com.zhny.project.txt.dataset.mapper.TxtDatasetMapper;
import com.zhny.project.txt.gather.domain.TxtGather;
import com.zhny.project.txt.gather.mapper.TxtGatherMapper;
import com.zhny.project.txt.gather.service.ITxtGatherService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 汇总显示Controller
 *
 * @author yanqun
 * @date 2020-01-04
 */
@Controller
@RequestMapping("/txt/gather")
public class TxtGatherController extends BaseController
{
    private String prefix = "txt/gather";

    @Autowired
    private ITxtGatherService txtGatherService;
    @Autowired
    private TxtDatasetMapper txtDatasetMapper;
    @Autowired
    private TxtGatherMapper txtGatherMapper;

    @RequiresPermissions("txt:gather:view")
    @GetMapping("/dataset/{datasetKey}")
    public String gather(@PathVariable("datasetKey") String datasetKey,ModelMap modelMap)
    {
        modelMap.put("datasetKey",datasetKey);
        return prefix + "/gather";

    }

    /**
     * 查询汇总显示列表
     */
    @RequiresPermissions("txt:gather:list")
    @RequestMapping("/list/{datasetKey}")
    @ResponseBody
    public TableDataInfo list(@PathVariable("datasetKey") String datasetKey, String fieldName )
    {
        startPage();
        TxtGather txtGather = new TxtGather();
        txtGather.setDatasetKey(datasetKey);
        txtGather.setFieldName(fieldName);
        List<TxtGather> list = txtGatherService.selectTxtGatherList(txtGather);
        return getDataTable(list);
    }

    /**
     * 导出汇总显示列表
     */
    @RequiresPermissions("txt:gather:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TxtGather txtGather)
    {
        List<TxtGather> list = txtGatherService.selectTxtGatherList(txtGather);
        ExcelUtil<TxtGather> util = new ExcelUtil<TxtGather>(TxtGather.class);
        return util.exportExcel(list, "gather");
    }

    /**
     * 新增汇总显示
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存汇总显示
     */
    @RequiresPermissions("txt:gather:add")
    @Log(title = "汇总显示", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TxtGather txtGather)
    {
        return toAjax(txtGatherService.insertTxtGather(txtGather));
    }

    /**
     * 修改汇总显示
     */
    @GetMapping("/edit/{gatherKey}")
    public String edit(@PathVariable("gatherKey") String gatherKey, ModelMap mmap)
    {
        TxtGather txtGather = txtGatherService.selectTxtGatherById(gatherKey);
        mmap.put("txtGather", txtGather);
        return prefix + "/edit";
    }

    /**
     * 修改保存汇总显示
     */
    @RequiresPermissions("txt:gather:edit")
    @Log(title = "汇总显示", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(String datasetKey)
    {
        return toAjax(txtGatherService.updateTxtGather(datasetKey));
    }

    /**
     * 删除汇总显示
     */
    @RequiresPermissions("txt:gather:remove")
    @Log(title = "汇总显示", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(txtGatherService.deleteTxtGatherByIds(ids));
    }

    /**
     * 修改计算出来的特征值范围
     */
    @RequiresPermissions("txt:gather:edit")
    @Log(title = "汇总显示", businessType = BusinessType.UPDATE)
    @RequestMapping("/saveList/{datasetKey}")
    public String editSaveDataset(@PathVariable("datasetKey") String datasetKey,ModelMap modelMap)
    {
        modelMap.put("datasetKey",datasetKey);
        txtGatherService.updateTxtGather(datasetKey);
        return prefix + "/gather";
    }



    }
